﻿using System;
using SlimDX;
//pix

namespace InsTron
{
    class Camera
    {
        public Vector3 Position;
        public Vector3 Up;
        public Vector3 Target;

        private Vector3 speed;
        private Vector3 acceleration;
        private Vector3 finalPosition;
        

        public Camera()
        {
            Position = new Vector3(0f, 0f, -Constants.PlatformSize);
            Up = new Vector3(0f, 1f, 0f);
            Target = new Vector3(0f, 1f, 0f);
            finalPosition = new Vector3(0f, 0f, 0f);
        }


        public Matrix CameraView
        {
            get
            {
                return Matrix.LookAtLH(Position, Target, Up);
            }
        }

        
        public void FollowTargetSmoothDeprecated(Vector3 targetPosition, Vector3 targetDirection)
        {
            Target = targetPosition + targetDirection * 4f;

            Vector3 positionBehind = targetPosition - targetDirection * 0.2f;
            positionBehind.Y += 2.0f;
            Position = 0.9f * Position + 0.1f * positionBehind;
        }


        public void FollowTargetSmooth(Vector3 targetPosition, Vector3 targetDirection, float time)
        {
            Target = targetPosition + targetDirection * 2;

            targetPosition.Y += 2.8f;

            acceleration = targetPosition - Position;
            speed = acceleration * time * 3.0f;

            Position = Position + speed / 1000f;
        }


        public void GoToCornerSmooth(Vector3 cornerPosition, float time)
        {
            if (finalPosition.Y == 0.0f)
                finalPosition = Position;

            cornerPosition = (cornerPosition + finalPosition) / 2.0f;
            cornerPosition.Y += Constants.StartingPositionShift / 2.0f;

            acceleration = cornerPosition - Position;
            acceleration /= (float)Math.Max(1.0, Math.Sqrt(acceleration.Length()));
            speed = acceleration * time;

            Position = Position + speed / 1000f;
        }


        public Camera InvertedCamera
        {
            get
            {
                Camera inverted = new Camera();
                inverted.Position = Position;
                inverted.Target = Target;
                inverted.Up = Up;
                InvertCameraY(inverted);
                return inverted;
            }
        }


        private void InvertCameraY(Camera camera)
        {
            camera.Position.Y *= -1;
            camera.Target.Y *= -1;
            camera.Up.Y *= -1;
        }
    }
}
